/**
Handel file upload forms
**/

function file_upload_handler(form_id,data) {
	
	responseObj=$.parseJSON(data);
	
	if(typeof responseObj.errors!='undefined') {
		display_errors(responseObj);
		return false;
	} else {
		if( $(".uploadifyQueueItem").last().length > 0 ) {
			$('#file_upload').uploadifySettings('scriptData',{'guide_id':responseObj.guide_id});
			$('#file_upload').uploadifyUpload();
		}
		return true;
	}
}


/**
	@description: This function will process ajax based errors return by your controller, this function uses cake's core validation function. To render validation errors you must set your controller to send header 404 error along json errors object
	@params
	data: returned json errors object like
		{"errors":{
			"ModelName":{"field_name":"Your custom error message here"},
			"AnotherModelName":{"another_field_name":"Your custom error message 2 here"}
		}}
**/
function display_errors (responseObject) {
	var error_html;
	if(typeof responseObject!='undefined') {
		$.each(responseObject.errors, function(key, val) {
			if (val instanceof Object) {
				$.each(val, function(key2, val2) {
					if (val2 instanceof Object) {
						$.each(val2, function(key3, val3) {
							if(typeof key2 != 'undefined' && typeof key3 != 'undefined' && typeof val3 != 'undefined') {
								var field_id = ucwords(key) + key2 + ucwords(key3);
								var field_error = val3;
								show_error(field_id,field_error);
							}
						});
					} else {
						if(typeof key2 != 'undefined' && typeof val2 != 'undefined') {
							var field_id = ucwords(key) + ucwords(key2);
							var field_error = val2;
							show_error(field_id,field_error);
						}
					}
				});
			} else {
				if(typeof key != 'undefined' && typeof val != 'undefined') {
					var field_id = ucwords(key);
					var field_error = val;
					show_error(field_id,field_error);
				}	
			}
		});
		return true;
	} else {
		return false;	
	}
}

/**
	@description: Create error div HTML
	@params
	field_id: error div element id
	field_error: error div element text
**/
function create_error_div(field_id, field_error) {
	return '<div id="' + field_id + 'Error" class="validator"><span class="arrow"></span><p>'+field_error+'</p></div>';
}

/**
	@description: Bind some effects to error div element
	@params 
	field_id:error div element id
**/
function bind_error_effects(field_id) {
	$('#' + field_id + 'Error').css({top: $("#"+field_id).offset().top - 30, left: $("#"+field_id).offset().left}).show().delay(2000).fadeOut('slow');	
}

/**
	@description: Capitalize word's first letter
	@params
	string:string text to be converted
**/
function ucwords(string) {
	if(isNaN(string))
		return string.charAt(0).toUpperCase() + string.slice(1);
}

/**/
function hide_errors() {
	$('.invalid').removeClass('invalid');
	$('.validator').each(function () {
		$(this).hide();
	});
}

/**/
function show_error(field_id, field_error) {
	if ($('#' + field_id + 'Error').length == 0 && typeof field_id != 'undefined' && typeof field_error != 'undefined') {
		error_html=create_error_div(field_id,field_error);
		$('#'+field_id).parent().append(error_html);
	}
	if($("#"+field_id).offset()!=null) {
		bind_error_effects(field_id);
		$('#'+field_id).addClass('invalid');
	}
}